CREATE OR REPLACE TRIGGER ATS_T_OF
  AFTER INSERT OR UPDATE on F_OF FOR EACH ROW
BEGIN

    INSERT INTO ATS_OF
    (COF_CODIOF, COF_COFPAI, COF_CODPRO, COF_DTPREV, COF_QTPROG, COF_QTREAL, COF_QTREFU, 
     COF_DTENCE, COF_ROTEIR, COF_OFPROG, COF_DATCAD, COF_STATUS, COF_DATINI, COF_STATCQ, 
     COF_OFCATI, COF_CODINP, COF_QTDEST, COF_DTENTR, COF_OFORIG, COF_SEQUEN, COF_QTPEDI, COF_ORIGEM)
    VALUES(:NEW.COF_CODIOF, :NEW.COF_COFPAI, :NEW.COF_CODPRO, :NEW.COF_DTPREV, :NEW.COF_QTPROG, :NEW.COF_QTREAL, :NEW.COF_QTREFU, 
     :NEW.COF_DTENCE, :NEW.COF_ROTEIR, :NEW.COF_OFPROG, :NEW.COF_DATCAD, :NEW.COF_STATUS, :NEW.COF_DATINI, :NEW.COF_STATCQ, 
     :NEW.COF_OFCATI, :NEW.COF_CODINP, :NEW.COF_QTDEST, :NEW.COF_DTENTR, :NEW.COF_OFORIG, :NEW.COF_SEQUEN, :NEW.COF_QTPEDI,1);
            
  EXCEPTION 
  WHEN DUP_VAL_ON_INDEX THEN
      UPDATE ATS_OF 
         SET COF_COFPAI = :NEW.COF_COFPAI,
             COF_CODPRO = :NEW.COF_CODPRO, 
             COF_DTPREV = :NEW.COF_DTPREV, 
             COF_QTPROG = :NEW.COF_QTPROG, 
             COF_QTREAL = :NEW.COF_QTREAL, 
             COF_QTREFU = :NEW.COF_QTREFU, 
             COF_DTENCE = :NEW.COF_DTENCE, 
             COF_ROTEIR = :NEW.COF_ROTEIR, 
             COF_OFPROG = :NEW.COF_OFPROG, 
             COF_DATCAD = :NEW.COF_DATCAD, 
             COF_STATUS = :NEW.COF_STATUS, 
             COF_DATINI = :NEW.COF_DATINI, 
             COF_STATCQ = :NEW.COF_STATCQ, 
             COF_OFCATI = :NEW.COF_OFCATI, 
             COF_CODINP = :NEW.COF_CODINP, 
             COF_QTDEST = :NEW.COF_QTDEST,
             COF_DTENTR = :NEW.COF_DTENTR, 
             COF_OFORIG = :NEW.COF_OFORIG, 
             COF_SEQUEN = :NEW.COF_SEQUEN, 
             COF_QTPEDI = :NEW.COF_QTPEDI, 
             COF_ORIGEM = 1 
      WHERE COF_CODIOF = :NEW.COF_CODIOF;                         

END;
/

